/********************************************************/
/*	Copyright (C) 2012 Gong Li Bin			*/
/*	Project:	MapReduce			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_05_25			*/
/*	File:		MrHubIns.cpp			*/
/********************************************************/

#include "MrHub.h"

#define	MR_HI_TOTAL			10
#define	MR_HI_PACKET			MR_PACKET * 100

int main(int argc, char* argv[])
{
	CMrPro CPro;
	CMrTcp CTcp;
	CMrTime CTime;
	int iCount = 1;
	int iLength = 0;
	char szKey[MR_HI_PACKET] = { 0 };
	char szVal[MR_HI_PACKET] = { 0 };
	UCHAR uszSend[MR_HI_PACKET] = { 0 };
	UCHAR uszRecv[MR_HI_PACKET] = { 0 };
	MRPROKV_S stKv[MR_MR_VAL] = {{MR_MR_KEY, 0, (UCHAR*)szKey}, {MR_MR_VAL, 0, (UCHAR*)szVal}};
	MRPROHEAD_S stHead = {
		MR_PRO_FLAG,
		MR_PRO_1,
		MR_PRO_DATA,
		MR_PRO_0,
		MR_PRO_Y,
		MR_PRO_0,
		MR_DEF_EHUB,
		MR_DEF_INST,
		MR_PRO_0,
		MR_DEF_SUCC,
		MR_MR_VAL,
		MR_PRO_0,
	};

	CTime.MrTimeOn();

	for (; iCount < MR_HI_TOTAL; iCount ++) {
		sprintf(szKey, "%d", iCount);
		sprintf(szVal, "Val: %d_", iCount);
		memset(szVal + strlen(szVal), 'A', MR_VALUE);

		stKv[0].m_iLength = strlen(szKey);
		stKv[1].m_iLength = strlen(szVal);

		CPro.MrProPutBody(NULL);
		CPro.MrProInduce(&stHead);
		CPro.MrProPutTotal(MR_MR_VAL);
		CPro.MrProPutLength(MrProKvOutput(stKv, MR_MR_VAL, &uszSend[MR_PRO_HEAD], MR_HI_PACKET - MR_PRO_HEAD));
		CPro.MrProCreate(uszSend, MR_HI_PACKET);

		if (MR_SUCCESS == CTcp.MrTcpConnect((char*)"172.16.8.211", (char*)"12001")) {
			CTcp.MrTcpSetLinger(0);
			CTcp.MrTcpSetNoDelay();
			if (CTcp.MrNetSendAll(uszSend, MR_PRO_HEAD + CPro.MrProGetLength()) > 0) {
				if ((iLength = CTcp.MrNetRecv(uszRecv, MR_HI_PACKET)) > 0) {
					CPro.MrProParse(uszRecv, iLength);
					CPro.MrProDisplay();
					if (MR_DEF_SUCC == CPro.MrProGetResult()) {
						MR_PRINT("Result[%08d]: success\n", iCount);
					}
					else {
						MR_PRINT("Result[%08d]: failure\n", iCount);
					}
				}
				else {
					MR_ERROR("Failed[%08d] to MrNetRecv\n", iCount);
				}
			}
			else {
				MR_ERROR("Failed[%08d] to MrNetSend\n", iCount);
			}
			CTcp.MrNetClose();
		}
		else {
			MR_ERROR("Failed[%08d] to MrTcpConnect\n", iCount);
		}
	}

	CTime.MrTimeOff();
	CTime.MrTimeDisplay();

	return MR_SUCCESS;
}
